# -*- coding:utf-8 -*-

import configparser
import os

'''
    [Config]
    StartHotKeyIndex
    StopHotKeyIndex
    LoopTimes
    Precision
    ExecuteSpeed
'''

conf = configparser.ConfigParser()


# 给config.ini设置默认参数
def setdefaultconf(config):
    config.add_section('Config')
    config.set('Config', 'StartHotKeyIndex', '3')
    config.set('Config', 'StopHotKeyIndex', '6')
    config.set('Config', 'LoopTimes', '1')
    config.set('Config', 'Precision', '200')
    config.set('Config', 'ExecuteSpeed', '100')


# 返回获取的config.ini中设置的值 
def getconfig():
    """返回的是一个元组list类型的集合

        通过conf.items()获取在config.ini文件下的参数, 并返回相应元组list集合
        例: conf.items('Config')[0][1] =========> 3
            即在config.ini文件下[Config]内容下, 第一个参数的value值为3
    """
    if not os.path.exists('config.ini'):
        setdefaultconf(conf)
        conf.write(open('config.ini', 'w'))
    else:
        conf.read('config.ini')

    # 观察getconfig()返回的内容
    # print('============== Config ==================')
    # print(conf.items('Config'))
    # config = conf.items('Config')
    # print(f'conf[0][1] =========> {config[0][1]}')
    # print('============== Config End ==============')
    
    return conf.items('Config')


# 存储config.ini的一些值
def saveconfig(newStartIndex, newStopIndex, newTimes, newPrecsion, newSpeed):
    conf.set('Config', 'StartHotKeyIndex', str(newStartIndex))
    conf.set('Config', 'StopHotKeyIndex', str(newStopIndex))
    conf.set('Config', 'LoopTimes', str(newTimes))
    conf.set('Config', 'Precision', str(newPrecsion))
    conf.set('Config', 'ExecuteSpeed', str(newSpeed))
    conf.write(open('config.ini', 'w'))
